package com.zzy.module.todo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzy.common.domain.RI;
import com.zzy.common.domain.RSD;
import com.zzy.common.entity.db.TodoDaoEntity;
import com.zzy.module.todo.entity.QqBack;
import com.zzy.module.todo.entity.QqList;
import com.zzy.module.todo.entity.TodoBack;
import com.zzy.module.todo.mapper.TodoMapper;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * @author zhuZhaoYang
 * @date 2021/12/5 14:14
 */
@RestController
@RequestMapping("/todo")
@Validated
public class TodoController {

    private TodoMapper todoMapper;

    @Autowired
    public void setTodoMapper(TodoMapper todoMapper) {
        this.todoMapper = todoMapper;
    }

    @ApiOperation(value = "查询已有todo的qq列表")
    @GetMapping("/getQqList")
    @RequiresPermissions("manager:super")
    public RI getQqList(@Min(value = 1, message = "页码不能小于1")
                        @NotNull(message = "页码不能为空")
                                Integer index,
                        @Min(value = 1, message = "每页条目不能少于1")
                        @NotNull(message = "每页条目不能为空")
                                Integer limit) {
        Page<QqList> page = new Page<>(index, limit);
        IPage<QqList> qqPage = todoMapper.getQqList(page);
        QqBack qqBack = new QqBack();
        qqBack.setList(qqPage.getRecords());
        qqBack.setCurIndex(qqPage.getCurrent());
        qqBack.setTotalPage(qqPage.getPages());
        return new RSD(qqBack);
    }

    @ApiOperation(value = "获取已完成待办")
    @GetMapping("/getFinishedTodo")
    public RI getFinishedTodo(@NotNull(message = "id不能为空") Integer id,
                              @Min(value = 1, message = "页码不能小于1")
                              @NotNull(message = "页码不能为空")
                                      Integer index,
                              @Min(value = 1, message = "每页条目不能少于1")
                              @NotNull(message = "每页条目不能为空")
                                      Integer limit) {
        Page<TodoDaoEntity> page = new Page<>(index, limit);
        IPage<TodoDaoEntity> todoPage = todoMapper.getFinishedTodoByQqId(id, page);
        TodoBack todoBack = new TodoBack();
        todoBack.setList(todoPage.getRecords());
        todoBack.setCurIndex(todoPage.getCurrent());
        todoBack.setTotalPage(todoPage.getPages());
        return new RSD(todoBack);
    }

    @ApiOperation(value = "获取未完成待办")
    @GetMapping("/getTodo")
    public RI getTodo(@NotNull(message = "id不能为空") Integer id,
                      @Min(value = 1, message = "页码不能小于1")
                      @NotNull(message = "页码不能为空")
                              Integer index,
                      @Min(value = 1, message = "每页条目不能少于1")
                      @NotNull(message = "每页条目不能为空")
                              Integer limit) {
        Page<TodoDaoEntity> page = new Page<>(index, limit);
        IPage<TodoDaoEntity> todoPage = todoMapper.getTodo(id, page);
        TodoBack todoBack = new TodoBack();
        todoBack.setList(todoPage.getRecords());
        todoBack.setCurIndex(todoPage.getCurrent());
        todoBack.setTotalPage(todoPage.getPages());
        return new RSD(todoBack);
    }


}
